
import { defaultConfig } from "../config/default_config";
import { Config } from "../types/config_type";
import { LoginRoomType, LogoutRoomType } from "../types/room_type";
import deviceDetectionUtils from "../utils/device_detection";
import throwError from "../utils/throwError";
import SFRTCRoom from "./room";


/**
 * sf_rtc
 */
class SFRTC {
  private _instance: SFRTC | null = null;
  private _roomInstance: SFRTCRoom | null = null;
  private _config: Config | null = null;

  constructor (config?: Config) {
    if (!this._instance) {
      this._instance = new SFRTC();
      this._config = {
        ...defaultConfig, 
        ...config
      }
    }

    if (!this._roomInstance) {
      this._roomInstance = new SFRTCRoom(this._config!)
    }

    return this._instance;
  }

  /**
   * 检测当前设备是否支持
   */
  deviceDetection () :Promise<boolean> {
    return deviceDetectionUtils()
  }
  /**
   * 加入房间
   */
  async loginRoom (options: LoginRoomType) :Promise<boolean> {
    if (!this._roomInstance) {
      return false;
    }

    return this._roomInstance?.loginRoom(options)
  }
  /**
   * 退出房间
   */
  async logoutRoom (options: LogoutRoomType) :Promise<boolean> {
    if (!this._roomInstance?.getLoginRoomStatus) {
      await throwError('未加入房间前不可调用')
      return false;
    }
    return this._roomInstance?.logoutRoom(options)
  }

  /**
   * 获取远端音视频流
   */

  /**
   * 本地预览音视频流
   * @param mountDom 挂载节点，能够被[querySelector] 获取到的节点
   */
  async localStartPreview (mountDom: string) {
    const dom =  document.querySelector(mountDom)

    if (!dom) {
      await throwError(`dom: ${mountDom} not found`)
    }

    return this._roomInstance?.localStartPreview(mountDom)
  }
  /**
   * 结束本地预览音视频流
   */
  localStopPreview () {
    return this._roomInstance?.localStopPreview()
  }

  /**
   * 推送本地音视频流
   */

  /**
   * 切换 前/后 置摄像头
   */

  /**
   * 关闭/打开摄像头
   */

  /**
   * 设置扬声器音量
   * * [0] 为静音状态
   * * [1] 为全音量状态
   */

  /**
   * 设置麦克风音量
   * * [0] 为静音状态
   * * [1] 为全音量状态
   */
  
  /**
   * 释放
   */
  dispose () : void {
    this._instance = null
    this._roomInstance = null
    this._config = null
  }
}



export default SFRTC